
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author：作者姓名
 * @Package：PACKAGE_NAME
 * @Project：newshop
 * @name：threadorder
 * @Date：2024/7/2 21:32
 */
public class threadorder {
    public static void main(String[] args) {
//        Thread t1 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("线程1");
//            }
//        },"线程1");
//        Thread t2 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("线程2");
//            }
//        },"线程2");
//        Thread t3 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("线程3");
//            }
//        });
//        Thread t1 = new Thread(() -> System.out.println("线程1"));
//        Thread t2 = new Thread(() -> System.out.println("线程2"));
//        Thread t3 = new Thread(() -> System.out.println("线程3"));
//        ExecutorService executor = Executors.newSingleThreadExecutor();
//        // 将线程依次加入到线程池中
//        executor.submit(t1);
//        executor.submit(t2);
//        executor.submit(t3);
//        // 及时将线程池关闭
//        executor.shutdown();
        try {
            Runnable task1 = () -> System.out.println("线程1");
            Runnable task2 = () -> System.out.println("线程2");
            Runnable task3 = () -> System.out.println("线程3");

            ExecutorService executor = Executors.newSingleThreadExecutor();

            // 提交任务给线程池
            executor.submit(task1);
            executor.submit(task2);
            executor.submit(task3);

            // 及时关闭线程池
            executor.shutdown();
        }catch (Exception e){
            System.out.println("错误");
        }
    }
}
